<template>
  <div class="container">
    <div class="center-container">
      <el-form :model="userForm1" :rules="rules" ref="userForm1" label-width="100px"
               class="demo-userForm">
        <span>基本信息1：</span>
        <el-form-item label="姓名" prop="name">
          <el-input v-model="userForm1.name"></el-input>
        </el-form-item>
      </el-form>
      <el-form :model="userForm2" :rules="rules" ref="userForm2" label-width="100px"
               class="demo-userForm">
        <span>基本信息2：</span>
        <el-form-item label="姓名" prop="name">
          <el-input v-model="userForm2.name"></el-input>
        </el-form-item>
      </el-form>
      <el-button type="primary" @click="submitForm()">保 存</el-button>
      <el-button @click="resetForm()">重 置</el-button>
    </div>


    <el-dialog
      title="提示"
      :visible.sync="centerDialogVisible"
      width="30%"
      @close="closeDialog"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      center>
      <span><pre>{{ userForm1 }}</pre></span>
      <span><pre>{{ userForm2 }}</pre></span>
      <span slot="footer" class="dialog-footer">
    <el-button @click="centerDialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="centerDialogVisible = false">确 定</el-button>
  </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "validate-01",
  data() {
    return {
      userForm1: {},
      userForm2: {},
      rules: {
        name: [
          {required: true, message: '请输入姓名', trigger: ['blur', 'change']},
          {min: 2, max: 5, message: '长度在 2 到 5 个字符', trigger: 'blur'}
        ],
      },
      centerDialogVisible: false,
    };
  },
  methods: {
    closeDialog() {
      this.centerDialogVisible = false;
      this.$refs.userForm1.resetFields();
      this.$refs.userForm2.resetFields();
    },
    submitForm() {
      const p1 = new Promise((resolve, reject) => {
        this.$refs['userForm1'].validate(valid => {
          if (valid) resolve()
        })
      })
      const p2 = new Promise((resolve, reject) => {
        this.$refs['userForm2'].validate(valid => {
          if (valid) resolve()
        })
      })
      Promise.all([p1, p2]).then(() => {
        console.log('submit')
        this.centerDialogVisible = true;
      })
    },
    resetForm() {
      this.$refs.userForm1.resetFields();
      this.$refs.userForm2.resetFields();
    }
  }
}
</script>

<style scoped>
.container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh; /* 设置父容器的高度为视口高度，使其充满整个屏幕 */
}

.center-container {
  max-width: 600px; /* 设置内部容器的最大宽度，根据需求进行调整 */
}
</style>
